﻿using DataAcquisitionBLL;
using DataAcquisitionDAL;

namespace DataAcquisition
{
    public partial class ProjectAddForm : BaseForm
    {
        #region 属性

        /// <summary>
        /// 试压模板
        /// </summary>
        public TemplateItem? templateObj;

        /// <summary>
        /// 逻辑处理
        /// </summary>
        private Project Service = new Project();

        #endregion

        #region 自定义方法

        /// <summary>加载模板信息
        /// 
        /// </summary>
        public void LoadTemplateInfo()
        {
            if (templateObj is not null)
            {
                Dictionary<string, Object> where = new Dictionary<string, Object>();
                where["number"] = templateObj.Number;
                List<TemplateModel> list = SqlSugarHelper.Inquireable<TemplateModel>(where);
                if (list.Count <= 0)
                {
                    Error("试压模板不存在，请重新选择");
                    templateObj = null;
                    return;
                }
                DesignPressureInput.Text = templateObj.DesignPressure.ToString();
                if (templateObj.PipePosition is not null)
                {
                    if (templateObj.PipePosition.Contains("站内/集输"))
                    {
                        TestTypeSelect.Text = "站内/集输";
                    }
                    else if (templateObj.PipePosition.Contains("站内"))
                    {
                        TestTypeSelect.Text = "站内";
                    }
                    else if (templateObj.PipePosition.Contains("集输"))
                    {
                        TestTypeSelect.Text = "集输";
                    }
                }
            }
            else
            {
                string msg = string.Format("请选择试压模板");
                Error(msg);
            }
            this.DesignPressureInput.Enabled = true;
        }

        #endregion

        #region 系统方法
        public ProjectAddForm()
        {
            InitializeComponent();
            Header.Text = "添加工程";
        }
        private void ConfirmButton_Click(object sender, EventArgs e)
        {
            ProjectModel model = new ProjectModel();
            model.Number = NumberInput.Text;
            model.Name = NameInput.Text;
            model.StakeMark = StakeMarkInput.Text;
            model.PipeSpecification = PipeSpecificationInput.Text;
            try
            {
                model.PipeLength = Convert.ToDouble(PipeLengthInput.Text);
            }
            catch (Exception)
            {
                model.PipeLength = -1;
            }
            model.Medium = MediumInput.Text;
            try
            {
                model.DesignPressure = Convert.ToDouble(DesignPressureInput.Text);
            }
            catch (Exception)
            {
                model.DesignPressure = -1;
            }
            model.PipeNature = PipeNatureSelect.Text;
            model.TestType = TestTypeSelect.Text;
            model.Company = CompanyInput.Text;
            model.ManometerPrecision = ManometerPrecisionSelect.Text;
            model.ManometerRange = ManometerRangeSelect.Text;
            if (templateObj is not null)
            {
                model.TemplateID = templateObj.Number;
            }
            else
            {
                model.TemplateID = -1;
            }

            Response response = Service.AddProject(model);
            if (response.Code != 200)
            {
                Error(response.Msg);
                return;
            }
            else
            {
                this.Close();
                if (this.Owner is not null)
                {
                    ProjectManageForm frm = (ProjectManageForm)this.Owner;
                    frm.LoadList();
                }
            }
        }

        private void DesignPressureInput_Click(object sender, EventArgs e)
        {
            TemplateSelectForm frm = new TemplateSelectForm();
            frm.Owner = this;
            frm.ShowDialog();
        }

        #endregion
    }
}
